package zpx.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import zpx.iservice.IMenuService;
import zpx.model.DataDictionaryItem;
import zpx.model.Material;
import zpx.model.Menu;
import zpx.model.User;
import zpx.util.XMLDDItemParser;


@Controller
@RequestMapping("/menu")
public class MenuController {
	
	private IMenuService menuService;

	public IMenuService getMenuService() {
		return menuService;
	}
	
	@Resource
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
	
	@RequestMapping(value="/goMenuList", method=RequestMethod.GET)
	public ModelAndView goMunuList(){
		Map<String, Object> map = new HashMap<String, Object>();
    	List<DataDictionaryItem> menulevel=XMLDDItemParser.getDataDictionaryItems("MenuLevel").getDataDictioanryItems();
    	
    	JSONArray jsonArray = JSONArray.fromObject(menulevel);
    	map.put("menulevel",jsonArray);
		return new ModelAndView("menu/menuList",map);
	}
	
	//调到菜单编辑或添加页面
	@RequestMapping(value="/menuAddorEdit", method=RequestMethod.GET)
	public ModelAndView menuAddorEdit(HttpServletRequest request,
            HttpServletResponse response){
		
		Menu menu=new Menu();
		String operWay=request.getParameter("operWay");
    	// 编辑
    	if(operWay.equals("2")){
    		String id=request.getParameter("id");
    		menu=menuService.load(id);
    	}
		Map<String, Object> map = new HashMap<String, Object>();
		List<DataDictionaryItem> menulevel=XMLDDItemParser.getDataDictionaryItems("MenuLevel").getDataDictioanryItems();
		
		JSONArray jsonArray = JSONArray.fromObject(menulevel);
		map.put("menulevel",jsonArray);
		map.put("menu",menu);
		return new ModelAndView("menu/menuAddorEdit",map);
	}
	
	 /**
     * 
     * 保存物料信息
     * @param request
     * @param response
     * @return
     */
	@RequestMapping(value="/saveMenu",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> saveMenu(HttpServletRequest request, Menu menu) {
		User loginUser = (User)request.getSession().getAttribute("loginUser");
		Map<String, Object> searchMap=new HashMap<String, Object>();
		searchMap.put("loginUser", loginUser);
		searchMap.put("menu", menu);
		menuService.saveOrUpdate(searchMap);
		Map<String, String> returnMap = new HashMap<String, String>();
        returnMap.put("info", "success");
		return returnMap;
	}

	/**
     * 获取当前用户的常用菜单
     * 
     * @return
     */
    @RequestMapping(value = "/getMenus", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public JSONArray getMenus(HttpServletRequest request, HttpServletResponse response) {
        JSONArray result = new JSONArray();
        List<Menu> menulist=menuService.pMenulist();
        while (menulist != null && menulist.size() > 0) {
        	Menu temp = menulist.get(0);
            JSONObject jsonObjT = new JSONObject();
            jsonObjT.put("id", temp.getId());
            jsonObjT.put("text", temp.getName());
            jsonObjT.put("level", temp.getLevel());
            jsonObjT.put("murl", temp.getMurl());
            jsonObjT.put("orderid", temp.getOrderid());
            JSONArray jsonObjC = new JSONArray();
            //递归方法获取父节点下的子节点
            jsonObjC=getChildrenMenus(jsonObjC,temp.getId());
            if(!jsonObjC.isEmpty()){
            	
            	jsonObjT.put("state", "closed");
            	jsonObjT.put("children", jsonObjC);
            }
            
            result.add(jsonObjT);
            menulist.remove(temp);
        	
        }
        
        
        return result;
    }
    
    public JSONArray getChildrenMenus(JSONArray jsoArr,String id){
    	List<Menu> menulist=menuService.cMenulist(id);	
    	while (menulist != null && menulist.size() > 0) {
    		JSONObject jsoObj=new JSONObject();
    		Menu temp = menulist.get(0);
    		jsoObj.put("id", temp.getId());
    		jsoObj.put("text", temp.getName());
    		jsoObj.put("level", temp.getLevel());
    		jsoObj.put("murl", temp.getMurl());
    		jsoObj.put("orderid", temp.getOrderid());
    		if(temp.getMurl()==""||temp.getMurl()==null){
    			JSONArray jsonObjT = new JSONArray();
    			jsonObjT=getChildrenMenus(jsonObjT,temp.getId());
    			if(!jsonObjT.isEmpty()){
    				jsoObj.put("children", jsonObjT);
    			}
    			
    		}else{
    			 JSONObject aObj = new JSONObject();
                 aObj.put("URL",temp.getMurl() );
    			jsoObj.put("attributes", aObj);
    		}
    		
    		menulist.remove(temp);
    		jsoArr.add(jsoObj);
    	}
    	return jsoArr;
    }
    
    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
}
